package com.xyoto.takeawayUser.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xyoto.takeawayUser.dto.ShopFrontDTO;
import com.xyoto.takeawayUser.enums.ShopEnum;
import com.xyoto.takeawayUser.exception.ExceptionUtils;
import com.xyoto.takeawayUser.mapper.ShopMapper;
import com.xyoto.takeawayUser.mapper.ShoppingMapper;
import com.xyoto.takeawayUser.mapper.ShoppingTypeMapper;
import com.xyoto.takeawayUser.service.ShopService;
import com.xyoto.takeawayUser.utils.PageUtils;
import com.xyoto.takeawayUser.utils.Result;
import com.xyoto.takeawayUser.vo.ProdSnapshotVo;
import com.xyoto.takeawayUser.vo.ShopDetailVo;
import com.xyoto.takeawayUser.vo.ShoppingTypeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName: 店铺Service接口
 * @Author: 挽风
 * @Date: 2022
 * @Copyright: 2022 by 挽风
 * @Description:
 **/

@Service
@Slf4j
public class ShopServiceImpl implements ShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private ShoppingMapper shoppingMapper;

    @Autowired
    private ShoppingTypeMapper shoppingTypeMapper;
    @Override
    public Result getAllShops(PageUtils pageDTO) {
        log.info("开始查询所有店铺列表");

        //分页查询商铺列表
        PageHelper.startPage(pageDTO.getPageNum(),pageDTO.getPageSize());
        List<ShopFrontDTO> listShopFront = shopMapper.getAllShops();
        PageInfo<ShopFrontDTO> shopListPage = new PageInfo<>(listShopFront);

        log.info("查询成功");
        return Result.success(shopListPage);
    }

    /**
     * 查询店铺详情下的商品快照列表
     * @param targetId
     * @return
     */
    @Override
    public Result getShopProdSnapshotList(Integer targetId) {
        List<ShoppingTypeVo> shoppingTypeVoList = shoppingTypeMapper.getShopMenuTypeList(targetId);
        if (shoppingTypeVoList.size() == 0){
            ExceptionUtils.error(ShopEnum.NOT_PROD_INFO);
        }

        // 查询菜单下的商品列表
        List<Integer> typeIds = shoppingTypeVoList.stream().map(ShoppingTypeVo::getId).collect(Collectors.toList());
        List<ProdSnapshotVo> prodSnapshotVoList = shoppingMapper.getShopProdSnapshotList(typeIds);
        Map<Integer, List<ProdSnapshotVo>> prodMap = prodSnapshotVoList.stream().collect(Collectors.groupingBy(ProdSnapshotVo::getShoppingTypeId));

        shoppingTypeVoList = shoppingTypeVoList.stream().peek(parent -> parent.setProdSnapshotVoList(
                prodMap.get(parent.getId()))).collect(Collectors.toList());

        return Result.success(shoppingTypeVoList);
    }

    /**
     * 查询店铺详情
     * @param targetId
     * @return
     */
    @Override
    public Result getShopDetail(Integer targetId) {
        // 查询店铺信息
        ShopDetailVo shopDetailVo = shopMapper.getShopDetail(targetId);
        if (shopDetailVo == null){
            ExceptionUtils.error(ShopEnum.NOT_SHOP_INFO);
        }
        return Result.success(shopDetailVo);
    }
}
